<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreProjectRequest;
use App\Models\Application;
use App\Models\Project;
use App\Models\ProjectInvoice;
use App\Models\ProjectOther;
use App\Models\ProjectUser;
use App\Models\RoleUser;
use App\Services\ProjectService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ProjectController extends Controller
{

    protected $ProjectService;
    public function __construct(ProjectService $ProjectService)
    {
        $this->ProjectService = $ProjectService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->ProjectService->get($search);
        //判断权限
        $user = Auth::user();
        $project_id = null;
        if($user->id != 1){
            $project_id = RoleUser::where('user_id',$user->id)->pluck('project_id');
            $sql->whereIn('id',$project_id);
        }

        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data,'project_id'=>$project_id]);
    }

    public function processess(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->ProjectService->processess($search);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function projectUserCost(Request $Request) {
        $data = ProjectUser::with('user')->with('role')->where('project_id',$Request->project_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectUserCostEdit(Request $Request) {
        $POSTONLY = ProjectUser::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->ProjectService->projectUserCostEdit($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectUserCostInsert(Request $Request) {
        $POSTONLY = ProjectUser::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->ProjectService->projectUserCostInsert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function getProjectUser(Request $Request) {
        $data = $this->ProjectService->getProjectUsers($Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectUser(Request $Request) {
        $id = $Request->id ?? 0;
        if($id){
            $data = $this->ProjectService->getUser($Request->project_id,$id);
        }else{
            $data = $this->ProjectService->getUsers($Request->project_id);
        }

        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectIsUser(Request $Request) {
        $data = $this->ProjectService->projectIsUser($Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function selectList(Request $Request) {
        $search = $Request->search;
        $type = Auth::user()->company_id > 0 ? 'company' : 'admin';
        $sql = $this->ProjectService->get($search,$type);
        $sql->select('id','title','status');
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Project::where('id',$Request->id)->with('users')->where('company_id',Auth::user()->company_id)->first()->toArray();
        $other = ProjectOther::where('project_id',$Request->id)->first();
        if($other){
            $other = $other->toArray();
            unset($other['id']);
            $data = array_merge($data,$other);
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreProjectRequest $Request)
    {
        $POSTONLY = Project::POSTONLY;
        $input = $Request->only($POSTONLY);

        $POSTONLY_ = ProjectOther::POSTONLY;
        $input_ = $Request->only($POSTONLY_);
        $input['company_id'] = Auth::user()->company_id;
        $input['push_id'] = Auth::user()->id;
        $data = $this->ProjectService->insert($input,$input_);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreProjectRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        //判断权限 非本企业不能操作
        $project = Project::where('id',$Request->id)->where('company_id',Auth::user()->company_id)->first();
        if(!$project){
            return response()->json(['code' => '404', 'msg' => '该记录不存在']);
        }

        $POSTONLY_ = ProjectOther::POSTONLY;
        $input_ = $Request->only($POSTONLY_);

        $POSTONLY = Project::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['push_id'] = Auth::user()->id;
        $input['id'] = $Request->id;
        $data = $this->ProjectService->update($input,$input_);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function bindTeamStore(Request $Request)
    {
        $project = Project::find($Request->project_id);
        $project->teams;
        foreach ($project->teams as $k => $v) {
            if($v->id == $Request->id){
                return response()->json(['code' => '500', 'msg' => '该班组已存在']);
            }
        }
        $project->teams()->attach($Request->id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$project]);
    }

    public function bindTeamDelete(Request $Request)
    {
        $project = Project::find($Request->project_id);
        $project->teams()->detach($Request->id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$project]);
    }

    public function bindTeam(Request $Request)
    {
        $project = Project::find($Request->project_id);
        $data = $project->teams;
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function updateSummary(Request $Request)
    {
        if(!$Request->project_id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Project::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->project_id;
        $data = $this->ProjectService->updateSummary($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectInvoice(Request $Request)
    {
        $data = ProjectInvoice::with('application')->with('project')->where('project_id',$Request->project_id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projectInvoiceInfo(Request $Request)
    {
        $data = ProjectInvoice::with('application')->with('project')->where('id',$Request->id)->first();
        $project = Project::select('id','title','status','price','tax_rate','address','tax_type')->where('id',$Request->project_id)->first();
        $last = null;
        //查询累计开票金额
        $project->invoice_amount = ProjectInvoice::where('project_id',$Request->project_id)->sum('price');
        $project->mix = number_format(($project->invoice_amount / $project->price) * 100,2);

        if($data){
            // where('status','approved')->

            $data->is = true;
            if($data->application){
                $data->is = $data->application->status != 'rejected' ? false : true;
            }
        }else{
            $last = ProjectInvoice::where('project_id',$Request->project_id)->orderBy('id','desc')->first();
        }

        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data,'project'=>$project,'last'=>$last]);
    }

    public function storeProjectInvoice(Request $Request)
    {
        $POSTONLY = ProjectInvoice::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $data = $this->ProjectService->insertProjectInvoice($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function updateProjectInvoice(Request $Request)
    {
        $POSTONLY = ProjectInvoice::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->ProjectService->updateProjectInvoice($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function invoiceReviewer(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $input = [];
        $input['id'] = $Request->id;
        $input['review_comment'] = $Request->review_comment;
        $input['status'] = $Request->status;
        $input['reviewer_id'] = Auth::user()->id;
        $data = $this->ProjectService->invoiceReviewer($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }



    public function projectBindUser(Request $Request)
    {
        if(!$Request->project_id){
            return response()->json(['code' => '404', 'msg' => '请选择项目']);
        }
        if(!$Request->users){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        $data = $this->ProjectService->projectBindUser($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Project::find($Request->id);
        if($data->company_id != Auth::user()->company_id){
            return response()->json(['code' => '404', 'msg' => '权限错误']);
        }
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function deleteProjectInvoice(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = ProjectInvoice::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

}
